package com.puzzlebazar.client.main.control;

import java.util.HashMap;

import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.main.view.NewPuzzleView;
import com.puzzlebazar.client.main.view.NewPuzzleView.NewPuzzleCallback;
import com.puzzlebazar.client.puzzleinfo.NewPuzzleInfo;
import com.puzzlebazar.client.user.UserModel;

public class NewPuzzleState extends MainState {
	
	public static final String STATE_TOKEN = "NewPuzzle";
	private static final String TUTORIAL = "Tut";
	
	private final MyNewPuzzleCallback newPuzzleCallback = new MyNewPuzzleCallback();

	private NewPuzzleView newPuzzleView = null;
	private boolean tutorial = false;
	private boolean isInState = false;
	
	public NewPuzzleState( UserModel model, MainView view, MainController controller ) {
		super( model, view, controller );
	}

	@Override
	public String getStateToken() {
		return STATE_TOKEN;
	}
	
	@Override
	public void enter() {
		isInState  = true;		
		setMainPanelTitle();
		
		if( newPuzzleView == null )
			newPuzzleView = new NewPuzzleView();
		newPuzzleView.resetState();

		view.setMainWidget( newPuzzleView );

		enableCallbacks();
	
	}

	/**
	 * Sets the title of the page to indicate if the user is creating a tutorial or a puzzle
	 */
	private void setMainPanelTitle() {
		if( tutorial )
			view.setMainPanelTitle( "New Tutorial" );
		else
			view.setMainPanelTitle( "New Puzzle" );
	}


	@Override
	public void exit() {
		view.setMainPanelTitle( null );
		disableCallbacks();
		isInState = false;
	}

	/**
	 * Get the current parameters of the state provided  the information
	 * 
	 * @param tutorial True if the new puzzle should be a tutorial
	 * @return The parameters of the state
	 */
	public static HashMap<String,String> getParams( boolean tutorial ) {
		final HashMap<String,String> params = new HashMap<String,String>();
		params.put( TUTORIAL, tutorial?"T":"F" );
		return params;
	}

	@Override
	public HashMap<String,String> getParams() {
		return getParams( tutorial );
	}

	/**
	 * Sets the for creating a new puzzle
	 * 
	 * @param tutorial True if the new puzzle should be a tutorial
	 */
	public void setParams( boolean tutorial ) {
		if( this.tutorial != tutorial ) {
			this.tutorial  = tutorial;
			if( isInState )
				setMainPanelTitle();
		}
	}

	@Override
	public void setParams( HashMap<String,String> params ) throws Exception {
		try {
			setParams(
					params.get( TUTORIAL ).equals("T") );
		}
		catch( Exception exception ) {
			setParams( false );
			throw new Exception( "Invalid parameters" );
		}
	}
	
	private class MyNewPuzzleCallback implements NewPuzzleCallback {

		@Override
		public void ok(NewPuzzleInfo newPuzzleInfo) {
			newPuzzleInfo.setTutorial( tutorial );
			controller.switchToEditNewPuzzle( newPuzzleInfo );
		}
		
		@Override
		public void cancel() {
			controller.backToPreviousState();
		}

	}

	/**
	 * Turns all callbacks on
	 */
	private void enableCallbacks() {
		newPuzzleView.addCallback( newPuzzleCallback );
	}

	/**
	 * Turns all defaults callbacks off
	 */
	private void disableCallbacks() {
		newPuzzleView.removeCallback( newPuzzleCallback );
	}

	@Override
	public void release() {
		super.release();
		disableCallbacks();
	}
}
